/*
 * Thread Demos
 * Spring 2013 TCSS 558
 * Daniel M. Zimmerman
 */

package threads;

/**
 * A class that demonstrates thread interrupts.
 * 
 * @author Daniel M. Zimmerman
 * @version Spring 2013
 */

public class Interruptee implements Runnable
{
  // Instance Methods
  
  /**
   * Repeatedly prints a message to standard output until interrupted.
   */
  public void run()
  {
    int i = 1;
    while (!Thread.interrupted())
    {
      System.out.println("No interrupt yet... " + i);
      i = i + 1;
    }

    System.out.println("Finally, an interrupt!");
  }
  
  // Main Method
  
  /**
   * Create a single thread, start it, and interrupt it.
   * 
   * @param the_args command line arguments, ignored.
   */
  public static void main(final String[] the_args)
  {
    final Thread thread = new Thread(new Interruptee());
    thread.start();
    try
    {
      Thread.sleep(10);
    }
    catch (final InterruptedException e)
    {
      // this should never happen
      
      System.err.println("Ouch, we were interrupted.");
    }
    thread.interrupt();
  }
}
